package br.com.ss.seguranca.controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import br.com.ss.seguranca.model.dao.SistemaDAO;
import br.com.ss.seguranca.model.entity.Sistema;
import br.com.ss.seguranca.validator.SistemaValidator;
import br.com.ss.seguranca.validator.ValidationException;

@Component("sistemaHome")
// @ManagedBean(name = "sistemaHome")
public class SistemaHome {

	private static final Logger logger = LoggerFactory
			.getLogger(SistemaHome.class);

	private Sistema sistema;
	private Sistema selected = new Sistema();
	private List<Sistema> sistemas;
	private Sistema sistemaSelected;

	public Sistema getSistemaSelected() {
		return sistemaSelected;
	}

	public void setSistemaSelected(Sistema sistemaSelected) {
		this.sistemaSelected = sistemaSelected;
	}

	@Autowired
	private SistemaDAO sistemaDAO;

	public String getMessage() {
		logger.debug("Returning message from sistema home bean");
		return "Hello from Spring";
	}

	public Sistema getSistema() {
		return sistema;
	}

	@PostConstruct
	public void init() {
		this.sistemas = sistemaDAO.findAll();
		this.sistema = new Sistema();
	}

	public String setarSistema() {

		System.out.println("sistema selecionado === " + this.sistema.getNome());
		this.sistemaSelected = (Sistema) sistemaDAO.find(this.sistema.getId());

		FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
				.put("sistemaSelected", this.sistema);

		Sistema sistemaSelected = (Sistema) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("sistemaSelected");

		return "/pages/sistema/listSistema.xhtml";

	}

	public void save() {

		try {
			//SistemaValidator.validarCampos(sistema);

			if (sistema.getId() == 0)
				sistemaDAO.save(this.sistema);
			else
				sistemaDAO.merge(this.sistema);
			
			this.sistema = new Sistema();
			
			invalidateSistemas();
			getSistemas();
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO, "Operacao realizada com sucesso",
							"Dados salvos com sucesso !"));
		} catch (ValidationException e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN, "Warnning", e
							.getMessage()));
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erro", e
							.getMessage()));
		}
	}

	public void remove() {
		System.out.println(sistema.toString());
		sistemaDAO.delete(this.selected);
		sistema = new Sistema();
		invalidateSistemas();
		getSistemas();
	}

	public String edit() {
		System.out.println(sistema.toString());
		sistema = this.selected;
		this.selected = new Sistema();
		return "/pages/sistema/sistema.xhtml";
	}

	public String clean() {
		this.sistema = new Sistema();
		return "/pages/sistema/sistema.xhtml";
	}

	private void invalidateSistemas() {
		sistemas = null;
	}

	public List<Sistema> getSistemas() {
		sistemas = sistemaDAO.findAll();
		return sistemas;

	}

	public Sistema getSelected() {
		return selected;
	}

	public void setSelected(Sistema selected) {
		this.selected = selected;
	}

	public void setSistema(Sistema sistema) {
		this.sistema = sistema;
	}

	public void setSistemas(List<Sistema> sistemas) {
		this.sistemas = sistemas;
	}
}
